﻿#region 命名空间

using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;

#endregion

namespace Sharp.Infrastructure
{
    /// <summary>
    ///     缓存帮助类
    /// </summary>
    public class CacheHelper
    {
        /// <summary>
        ///     新增缓存
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="datetime"></param>
        public static void Add<T>(string key, T value, int datetime)
        {
            Add<T>(key, value, DateTimeType.Second, datetime, CacheItemPriority.NeverRemove);
        }

        /// <summary>
        ///     新增缓存
        /// </summary>
        /// <typeparam name="T"> </typeparam>
        /// <param name="key"> </param>
        /// <param name="value"> </param>
        /// <param name="datetimetype"> </param>
        /// <param name="datetime"> </param>
        public static void Add<T>(string key, T value, DateTimeType datetimetype, int datetime)
        {
            Add<T>(key, value, datetimetype, datetime, CacheItemPriority.NeverRemove);
        }



        /// <summary>
        ///     新增缓存
        /// </summary>
        /// <typeparam name="T"> </typeparam>
        /// <param name="key"> </param>
        /// <param name="value"> </param>
        /// <param name="datetimetype"> </param>
        /// <param name="datetime"> </param>
        /// <param name="cacheItemPriority"> </param>
        public static void Add<T>(string key, T value, DateTimeType datetimetype, int datetime,
            CacheItemPriority cacheItemPriority)
        {
            if (value == null)
            {
                return;
            }
            double seconds = datetime * (int)datetimetype;

            //HttpRuntime.Cache.Add(key, value, file, DateTime.Now.AddSeconds(seconds), TimeSpan.Zero, cacheItemPriority, null);

            IMemoryCache mc = new MemoryCache(Options.Create(new MemoryCacheOptions()));
            ICacheEntry ce = mc.CreateEntry(key);
            ce.Value = value;
            ce.AbsoluteExpiration = DateTime.Now.AddSeconds(seconds);
            ce.Priority = cacheItemPriority;
        }


        /// <summary>
        ///     获取缓存
        /// </summary>
        /// <typeparam name="T"> </typeparam>
        /// <param name="key"> </param>
        /// <returns> </returns>
        public static T Get<T>(string key)
        {
            IMemoryCache mc = new MemoryCache(Options.Create(new MemoryCacheOptions()));
            object o = mc.Get(key);
            if (o == null)
            {
                return default(T);
            }
            else
            {
                return (T)o;
            }
        }

        /// <summary>
        ///     获取缓存
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool TryGet<T>(string key, out T value)
        {
            IMemoryCache mc = new MemoryCache(Options.Create(new MemoryCacheOptions()));

            return mc.TryGetValue<T>(key, out value);
        }

        /// <summary>
        ///     清空缓存
        /// </summary>
        public static void Clear()
        {
            //HttpRuntime.Close();
        }

        /// <summary>
        ///     移除缓存
        /// </summary>
        /// <param name="key">缓存id</param>
        public static void Remove(string key)
        {
            IMemoryCache mc = new MemoryCache(Options.Create(new MemoryCacheOptions()));
            mc.Remove(key);

        }

        /// <summary>
        ///     模糊移除缓存
        /// </summary>
        /// <param name="key">缓存id</param>
        public static void RemoveLike(string key)
        {

        }
    }

    /// <summary>
    ///     缓存时间方式
    /// </summary>
    public enum DateTimeType
    {
        /// <summary>
        ///     秒
        /// </summary>
        Second = 1,

        /// <summary>
        ///     小时
        /// </summary>
        Hour = 3600,

        /// <summary>
        ///     天数
        /// </summary>
        Day = 86400,
    }
}